﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Bll;
using CSP.Bll.IObjectBlls;
using CSP.Common.Web;
using CSP.Web.UserControls;
using CSP.Web.Master;


namespace CSP.Web.MyHome.Authentication
{
    public partial class Detail : CSPMyHome
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //指定被选中的导航
            ((HomeMaster)Master).SelectedNavigationLink = MyHomeLeftNavigationLink.Authentication;

            InitUI();
        }


        /// <summary>
        /// 初始化UI
        /// </summary>
        private void InitUI()
        {
            CSP.Model.CSP.Authentication authentication = GetModifyAuthentication();
            this.LBName.Text = authentication.Name;
            this.LBDescription.Text = authentication.Description;
            this.CertificateImg.Src = "~/" + authentication.Filepath + "s_" + authentication.Filename;
        }

        /// <summary>
        /// 获取需要修改的认证信息
        /// </summary>
        /// <returns></returns>
        private CSP.Model.CSP.Authentication GetModifyAuthentication()
        {
            CSP.Model.CSP.Authentication authentication = null;
            CSPBllFactory factory = CSPBllFactory.GetInstance();
            using (IAuthenticationBll authenticationBll = factory.GetAuthenticationBll())
            {
                try
                {
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    authentication = authenticationBll.GetByIdAndUser(id, UserProvider.UserName);

                }
                catch (Exception ex)
                {
                    LoggerProvider.Error(authentication, ex);
                    this.FailRedirect("操作失败", "/MyHome/Authentication/List.aspx");
                }
            }

            if (authentication == null)
            {
                Response.Write("<script>alert('权限不足');</script>");
                throw new Exception("权限不足");
            }

            return authentication;
        }
    }
}